package com.lgx.right.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.exception.DataException;
import com.lgx.common.tree.TreeUtils;
import com.lgx.common.util.LayerOrderUtils;
import com.lgx.right.dao.IResourceDao;
import com.lgx.right.entity.Resource;
import com.lgx.right.service.IResourceService;

@Service("resourceService")
public class ResourceService implements IResourceService {

	@Autowired
	private IResourceDao resourceDao;
	
	@Autowired
	private GeneralJdbcDao baseDao;
	
	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}

	public IResourceDao getResourceDao() {
		return resourceDao;
	}

	public void setResourceDao(IResourceDao resourceDao) {
		this.resourceDao = resourceDao;
	}

	public Resource createResource(Resource resource) throws DataException {
		if(resource.getParent() != null)
		{
			String layerOrder = LayerOrderUtils.createLayerOrder("TS_RESOURCE", resource.getParent().getId()+"", this.baseDao);
			resource.setLayerOrder(layerOrder);
		}
		return resourceDao.createResource(resource);
	}

	public Resource updateResource(Resource resource) throws DataException {
		return resourceDao.updateResource(resource);
	}

	public void deleteResource(Resource resource) throws DataException {
		resourceDao.deleteResource(resource);
	}

	public void deleteResource(long id) throws DataException {
		resourceDao.deleteResource(id);
	}

	public Resource findResourceById(long id) {
		return resourceDao.findResourceById(id);
	}

	public List<Resource> findResourceList(String sql) {
		return resourceDao.findResourceList(sql);
	}

	@SuppressWarnings("unchecked")
	public List<Resource> findUserResourceList(long userId) {
		
		/**
		 * SELECT DISTINCT R.*
			  FROM TS_RESOURCE R
			  LEFT JOIN TS_RESOURCE RE
			    ON RE.LAYERORDER LIKE CONCAT(R.LAYERORDER, '%')
			 INNER JOIN TS_PERMISSION PE
			    ON RE.PERMISSION_ID = PE.ID
			 INNER JOIN TS_ROLE_PERMISSION RP
			    ON PE.ID = RP.PERMISSION_ID
			 INNER JOIN TS_ROLE RO
			    ON RP.ROLE_ID = RO.ID
			 INNER JOIN TS_USER_ROLE UR
			    ON RO.ID = UR.ROLE_ID
			 WHERE R.ISDELETE = '0'
			   AND R.PARENT IS NOT NULL
			   AND PE.ISDELETE = '0'
			   AND UR.USER_ID = 1
			 ORDER BY R.LAYERORDER
		 */
		String sql =" SELECT DISTINCT R.* "+
					"   FROM TS_RESOURCE R "+
					"   LEFT JOIN TS_RESOURCE RE "+
					"     ON RE.LAYERORDER LIKE CONCAT(R.LAYERORDER, '%') "+
					"  INNER JOIN TS_PERMISSION PE "+
					"     ON RE.PERMISSION_ID = PE.ID "+
					"  INNER JOIN TS_ROLE_PERMISSION RP "+
					"     ON PE.ID = RP.PERMISSION_ID "+
					"  INNER JOIN TS_ROLE RO "+
					"     ON RP.ROLE_ID = RO.ID "+
					"  INNER JOIN TS_USER_ROLE UR "+
					"     ON RO.ID = UR.ROLE_ID "+
					"  WHERE R.ISDELETE = '0' "+
					"    AND R.PARENT IS NOT NULL "+
					"    AND PE.ISDELETE = '0' "+
					"    AND UR.USER_ID = "+ userId +
					"  ORDER BY R.LAYERORDER ";
		
		return (List<Resource>) baseDao.queryJdbcForList(sql, Resource.class);
	}

	@SuppressWarnings("unchecked")
	public List<Resource> findResourceList() {
		String sql =" SELECT RE.* "+
					"   FROM TS_RESOURCE RE "+
					"  WHERE RE.ISDELETE = '0' "+
					//"    AND RE.PARENT IS NOT NULL "+
					"    AND RE.ISDELETE = '0' "+
					"  ORDER BY RE.LAYERORDER ";
		List<Resource> rs = (List<Resource>) baseDao.queryJdbcForList(sql, Resource.class);
		return (List<Resource>) TreeUtils.addChildren(rs, null, null);
	}

}
